package com.baoyanghui.servlet.merchant;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.baoyanghui.common.InterfaceAccess;
import com.baoyanghui.config.Configs;

/**
 * Servlet implementation class AddPlaceCoop
 */
public class AddPlaceCoop extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPlaceCoop() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("请使用POST方式调用");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject result;
		int code = 9999;
		String message = "";
		Map<String, String> params = new HashMap<String, String>();
		
		try {
			request.setCharacterEncoding("UTF-8");
			params.put("place_ids", request.getParameter("place_ids"));
			params.put("merchant_id", (String)request.getSession().getAttribute(Configs.SESSION_USER_ID));
			params.put("usercode", (String)request.getSession().getAttribute(Configs.SESSION_USERCODE));
			
			result = InterfaceAccess.callInterface("/merchant/add_place_coop", params);
			code = result.getInt("code");
			message = result.getString("message");
	
			if (code == 0) {
				response.sendRedirect(request.getContextPath() + "/merchant/manage_info.jsp");
			} else {
				request.getSession().setAttribute("error_message", message);
				response.sendRedirect(request.getContextPath() + "/merchant/manage_info.jsp?tab=add_place_coop");
			}
		} catch (IOException e) {
			response.sendRedirect(request.getContextPath() + "/merchant/manage_info.jsp?tab=add_place_coop");
		}
	}

}
